# FileUtil工具类
import os.path
import signal
import uuid

from backend import settings


class FileUtil:
    @staticmethod
    def unique_filename():
        unique_filename = str(uuid.uuid4())
        return unique_filename

    @staticmethod
    def read_file(target, keep = False):
        try:
            with open(target, 'r') as file:
                content = ""
                line = file.readline()
                while line:
                    content += line
                    if keep:
                        content += '\n'
                    line = file.readline()
            return content
        except:
            return False

    @staticmethod
    def write_file(file_name, data):
        try:
            with open(file_name, 'w') as file:
                file.write(data)
                return True
        except:
            return False

    @staticmethod
    def remove_tempfile(file_name, suffix):
        src = PathUtil.src(file_name, suffix)
        if os.path.exists(src):
            os.remove(src)

        compile_error = PathUtil.compiler_error(file_name)
        if os.path.exists(compile_error):
            os.remove(compile_error)

        excute = PathUtil.exe(file_name)
        if os.path.exists(excute):
            os.remove(excute)

        stdin = PathUtil.stdin(file_name)
        if os.path.exists(stdin):
            os.remove(stdin)

        stdout = PathUtil.stdout(file_name)
        if os.path.exists(stdout):
            os.remove(stdout)

        stderr = PathUtil.stderr(file_name)
        if os.path.exists(stderr):
            os.remove(stderr)


temp_path = "./static/temp"


class PathUtil:
    @staticmethod
    def add_suffix(file_name, suffix):
        path_name = temp_path
        path_name = os.path.join(path_name, file_name + suffix)
        return path_name

    @staticmethod
    def src(file_name, suffix):
        return PathUtil.add_suffix(file_name, suffix)

    @staticmethod
    def exe(file_name):
        return PathUtil.add_suffix(file_name, ".exe")

    @staticmethod
    def compiler_error(file_name):
        return PathUtil.add_suffix(file_name, ".compiler_error")

    @staticmethod
    def stdin(file_name):
        return PathUtil.add_suffix(file_name, ".stdin")

    @staticmethod
    def stdout(file_name):
        return PathUtil.add_suffix(file_name, ".stdout")

    @staticmethod
    def stderr(file_name):
        return PathUtil.add_suffix(file_name, ".stderr")


class CodeUtil:
    @staticmethod
    def code_to_desc(code, file_name):
        desc = ""
        if code == 0:
            desc = "编译运行成功"
        elif code == -1:
            desc = "提交的代码是空"
        elif code == -2:
            desc = "未知错误"
        elif code == -3:
            # 编译失败
            desc = FileUtil.read_file(PathUtil.compiler_error(file_name), True)           
        elif code == signal.SIGABRT:
            desc = "超出内存限制"
        elif code == signal.SIGXCPU:
            desc = "超出时间限制"
        else:
            desc = "运行失败"
        return desc
